/**
 * Created by 熊超超 on 2018/7/2.
 */
/**
 * 本地环境
 */
// var baseUrl = 'http://5062db1e.ngrok.io/VDS/wechat';
var baseUrl = ' http://10.108.100.125:3000/v1/public'
/**
 * 测试环境
 */
// var baseUrl = 'http://vdstest.changan.com.cn/VDS/wechat'

// 授权
function getOpenid(code, url, cb, cb2) {
  request('get', '/oauth2getAccessToken.do', {code: code}, cb, cb2, true)
}
function getDriverId(cb, cb2) {
    request('get', '/getdriverId.do',{}, cb, cb2, true)
}
// 绑定用户
function bind(params, cb, cb2) {
  request('post', '/inv_driver_bind_weixin.do', params, cb, cb2)
}
// url签名
function getSign(params, cb, cb2) {
  request('post', '/oauth2getSignature.do', params, cb, cb2, true)
}
// 交接单列表
function getList(cb) {
  request('post', '/tms_send_car_list.do', {
    driverId: window.localStorage.getItem('driverId')
  }, cb)
}
// 交接单详情
function getDetail(params, cb) {
  request('post', '/query_tms_send_car_detail.do', params, cb)
}
// 到达申请
function sendCarApply(params, cb) {
  request('post', '/tms_send_car_apply.do', params, cb)
}
// 异常类别
function getTypes(params, cb) {
  request('post', '/inv_look_up_types_query.do', params, cb)
}
// 异常报送
function saveAbnormal(params, cb) {
  request('post', '/tms_abnormity_app_save.do', params, cb)
}
function getAddress(params,cb){
    $.ajax({
        type: 'GET',
        url: 'https://apis.map.qq.com/ws/geocoder/v1/',
        data: params,
        contentType: 'application/json',
        dataType: 'jsonp',
        // timeout: 45000,
        success: function(data){
            console.log(data)
            if(data.status==0){
                // console.log(data);
                cb(data);
            }
        },
        error: function(xhr, type){
            console.log('e',xhr)
        }
    })
}
// 统一请求方法
function request(type, url, data, cb, cb2, hideLoading) {
  var loading = null
  if (!hideLoading) {
    loading = weui.loading('请稍候...');
  }
  $.ajax({
    type: type.toUpperCase(),
    url: baseUrl + url,
    data: type === 'get' ? data : JSON.stringify(data),
    contentType: 'application/json',
    dataType: 'json',
    headers: {wechatCode: window.localStorage.getItem('openid') || ''},
    // timeout: 45000,
    success: function(data){
      loading && loading.hide()
      if (data.status == 200) {
       // console.log(data)
        cb && cb(data)
      } else if (data.status == 402) { // 返回402表示授权过期，要重新授权
        auth(window.location.href)
      } else if (data.status == 403) { // 返回402标识没有绑定用户，跳转到绑定页面
          weui.alert("您尚未注册驾驶员，请前往注册");
    	  window.location.href = 'reg.html?timeStamp'+new Date().getTime();
      } else {
        if (cb2) {
          cb2(data.message)
        } else {
          weui.alert(data.message);
        }
      }
    },
    error: function(xhr, type){
      loading && loading.hide()
      if (cb2) {
        cb2()
      } else {
        weui.alert('服务器错误');
      }
    }
  })
}